package com.aaa.reflect;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.ann.MyAnn;
import com.sun.org.apache.xpath.internal.SourceTree;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/6/14 8:40
 * @description：
 * @modified By：
 * @version:
 */
public class Test {
    public static void main(String[] args) throws Exception {
      /*  Person person1 = new Person(100,"张三");
        System.out.println(person1.toString());
        Person person2 = new Student(100,"张三",290);
        System.out.println(person2.toString());*/
       /* //1、通过类名(全路径名)获取class对象com.aaa.reflect.Person

        try {
            Class<?> clazz1 = Class.forName("com.aaa.reflect.Person");
            System.out.println(clazz1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2、通过.class的方式获取class对象
        Class<Person> clazz2 = Person.class;
        System.out.println(clazz2);
        //3、通过对象的getClass方法获取
        Person person= new Person();
        //泛型上限  小于等于person
        Class<? extends Person> clazz3 = person.getClass();
        System.out.println(clazz3);*/
        System.out.println("------------------------------------------------");
        Class<Person> clazz = Person.class;
/*        String clazzName = clazz.getName();
        String packageName = clazz.getPackage().getName();
        String simpleName = clazz.getSimpleName();
        System.out.println(clazzName);
        System.out.println(packageName);
        System.out.println(simpleName);*/
        //创建一个实例对象
       /* try {
            //调用的是无参的构造器
            Person person = clazz.newInstance();
            System.out.println(person.toString());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }*/
        //泛型下限，大于等于person
      /*  Class<? super Person> superclass = clazz.getSuperclass();
        System.out.println(superclass);*/
        //获取注解
      /*  Annotation[] annotations = clazz.getAnnotations();
        System.out.println(annotations.length);
        for (Annotation annotation : annotations) {
            System.out.println(annotation.toString());
        }*/
        //获取接口
      /*  Class<?>[] interfaces = clazz.getInterfaces();
        System.out.println(interfaces.length);*/
        System.out.println("------------------------------------------------");
/*            //获取公有的属性
            Field field = clazz.getField("name");
            //获取私有属性
            Field fieldId = clazz.getDeclaredField("id");
            Field fieldName = clazz.getDeclaredField("name");
            System.out.println(field.getName());
            System.out.println(fieldId.getName());
            System.out.println(fieldName.getName());*/
           /* Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                System.out.println(field);
            }*/

            /* Person person = new Person(1,"admin");
             Person person1 = clazz.newInstance();
             System.out.println(person.equals(person1));*/
/*            Field name1 = clazz.getField("name");
            Field name2 = clazz.getField("name");
            boolean equals = name1.equals(name2);
            System.out.println(equals);*/
        /*Person person = new Person(1, "admin");
        Field name1 = clazz.getDeclaredField("name");
        name1.setAccessible(true);
        //传入一个对象，通过属性对象获取属性值
        Object o = name1.get(person);
        System.out.println(o);
        System.out.println(person);
        System.out.println("======================");
        //设置新值
        name1.set(person, "xxxx");
        System.out.println(person);*/
        System.out.println("------------------------------------------------");
      /*  Method method = clazz.getMethod("introduce");
        System.out.println(method);*/
      /*  Method add = clazz.getDeclaredMethod("add", int.class, int.class,int.class);
       *//* System.out.println(add);*//*
        //通过反射的方式调用方法
        Person object = new Person();
        //如果是私有方法，需要下面的代码
        add.setAccessible(true);
        Object invoke = add.invoke(object, 1, 2, 3);
        System.out.println(invoke);*/

       /* Method calc = clazz.getDeclaredMethod("calc", int.class, int.class, String.class);
        Object invoke = calc.invoke(new Person(), 12, 24, "*");
        System.out.println(invoke);*/

     /*   Method[] declaredMethods = clazz.getDeclaredMethods();
       *//* System.out.println(declaredMethods.length);*//*
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }*/
      /*  Person person = new Person();*/
        System.out.println("------------------------------------------------");
    /*    Constructor<Person> personConstructor = clazz.getConstructor();
        System.out.println(personConstructor);
        Constructor<Person> constructor = clazz.getConstructor(int.class, String.class);
        System.out.println(constructor);*/
       /*Person p1= new Person();
       Person p2= new Person();*/
     /*   Person p1= Person.getPerson();
        Person p2=Person.getPerson();
        System.out.println(p1==p2);*/
/*        Constructor<Person> constructor = clazz.getConstructor(int.class, String.class);
     //   System.out.println(constructor);
        Person person = constructor.newInstance(12,"admin");
        System.out.println(person.toString());*/
        System.out.println("------------------------------------------------");
      /*  Class<?>[] interfaces = clazz.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }*/
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        MyAnn myAnn = clazz.getAnnotation(MyAnn.class);
        System.out.println(myAnn.name());
    }
}
